﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XStudio.XCRF.Data.Entities;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using XStudio.XCRF.Data.Enums;
using XStudio.XCRF.Data.Entities.BasicInfo;
using XStudio.XCRF.Data.Entities.LabCheck;
using XStudio.XCRF.Data.Entities.CureInfo;
using XStudio.XCRF.Web.Models.Enums;

namespace XStudio.XCRF.Web.Export
{
    public class ExportHelper
    {
        private List<String> paths = new List<String>();

        public Boolean exportCRFs(List<CRF> crfs)
        {
            paths.Clear();
            foreach (CRF crf in crfs)
            {
                generateCRFDoc(crf);
            }

            string strZipPath = HttpContext.Current.Server.MapPath("~/Export/temp/病例.zip");
            string strZipTopDirectoryPath = HttpContext.Current.Server.MapPath("~/Export/temp/");
            int intZipLevel = 6;
            string strPassword = "";
            string[] filesOrDirectoriesPaths = paths.ToArray();
            ZipHelper helper = new ZipHelper();
            helper.Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);

            FileInfo fileInfo = new FileInfo(strZipPath);
            HttpResponse Response = HttpContext.Current.Response;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("病例" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".zip", System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.ContentType = "application/octet-stream";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.WriteFile(fileInfo.FullName);
            Response.Flush();
            Response.End();
            return true;
        }

        private Boolean generateCRFDoc(CRF crf)
        {
            if (crf == null)
                return false;

            String path = HttpContext.Current.Server.MapPath("~/Export/temp/" + crf.Patient.Name + ".doc");
            FileStream afile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(afile, System.Text.Encoding.GetEncoding("GB2312"));
            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
            Panel panel = createCRFPanel(crf);
            panel.RenderControl(textWriter);
            sw.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body lang=ZH-CN>" + stringWriter.ToString());
            sw.Close();

            paths.Add(path);
            return true;
        }

        private Panel createCRFPanel(CRF crf)
        {
            Panel panel = new Panel();

            Panel p_title = new Panel();
            p_title.Width = Unit.Parse("100%");
            p_title.HorizontalAlign = HorizontalAlign.Center;
            Label lbl_general = new Label();
            lbl_general.Font.Bold = true;
            lbl_general.Font.Size = FontUnit.Larger;
            lbl_general.Text = "一般资料";
            p_title.Controls.Add(lbl_general);
            panel.Controls.Add(p_title);

            Table table = new Table();
            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.Width = Unit.Parse("100%");
            TableRow row1 = new TableRow();
            row1.Cells.Add(createCell("姓名：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row1.Cells.Add(createCell(crf.Patient.Name, false, Unit.Parse("15%"), HorizontalAlign.Left));
            row1.Cells.Add(createCell("就诊科室：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row1.Cells.Add(createCell(crf.Patient.TreatDepart, false, Unit.Parse("15%"), HorizontalAlign.Left));
            row1.Cells.Add(createCell("名族：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row1.Cells.Add(createCell(crf.Patient.Nationality, false, Unit.Parse("15%"), HorizontalAlign.Left));
            row1.Cells.Add(createCell("地址：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row1.Cells.Add(createCell(crf.Patient.Address, false, Unit.Parse("15%"), HorizontalAlign.Left));
            table.Rows.Add(row1);
            TableRow row2 = new TableRow();
            row2.Cells.Add(createCell("工作单位：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row2.Cells.Add(createCell(crf.Patient.Company, false, Unit.Parse("15%"), HorizontalAlign.Left));
            row2.Cells.Add(createCell("工种：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row2.Cells.Add(createCell(crf.Patient.JobKind, false, Unit.Parse("15%"), HorizontalAlign.Left));
            row2.Cells.Add(createCell("婚姻状况：", false, Unit.Parse("10%"), HorizontalAlign.Right));
            row2.Cells.Add(createCell(MarriageTypeInfo.GetName(crf.Patient.Marriage), false, Unit.Parse("15%"), HorizontalAlign.Left));
            table.Rows.Add(row2);
            panel.Controls.Add(table);

            panel.Controls.Add(createGeneralTabel(crf));

            if (crf.LabChecks != null && crf.LabChecks.Count > 0)
            {
                LabCheckPanelCreator lcpc = new LabCheckPanelCreator();
                foreach (LabCheck lc in crf.LabChecks)
                    panel.Controls.Add(lcpc.createLabCheckPanel(lc));
            }

            if (crf.CureInfoes != null && crf.CureInfoes.Count > 0)
            {
                CureInfoPanelCreator cipc = new CureInfoPanelCreator();
                foreach (CureInfo ci in crf.CureInfoes)
                    panel.Controls.Add(cipc.createCureInfoPanel(ci));
            }

            return panel;
        }

        //一般资料
        private Table createGeneralTabel(CRF crf)
        {
            Table table = new Table();
            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.Width = Unit.Parse("100%");

            TableRow row1 = new TableRow();
            row1.Cells.Add(createCell("性别：" + GenderTypeInfo.GetName(crf.Patient.Gender).ToString(), true, Unit.Parse("25%"), HorizontalAlign.Left));
            row1.Cells.Add(createCell("出生日期：" + crf.Patient.Birthday.ToString(), true, Unit.Parse("25%"), HorizontalAlign.Left));
            String height = "";
            if(crf.BasicInfoes != null &&  crf.BasicInfoes.Count > 0)
                height = crf.BasicInfoes.First().Height.ToString();
            row1.Cells.Add(createCell("身高：" + height + "cm", true, Unit.Parse("25%"), HorizontalAlign.Left));
            String weight = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                weight = crf.BasicInfoes.First().Weight.ToString();
            row1.Cells.Add(createCell("体重：" + weight + "kg", true, Unit.Parse("25%"), HorizontalAlign.Left));
            table.Rows.Add(row1);

            TableRow row2 = new TableRow();
            row2.Cells.Add(createCell("电话：" + crf.Patient.Phone, true, Unit.Parse("25%"), HorizontalAlign.Left));
            String source = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                source = SourceTypeInfo.GetName(crf.BasicInfoes.First().Source).ToString();
            TableCell source_cell = createCell("病例来源：" + source, true, Unit.Parse("25%"), HorizontalAlign.Left);
            source_cell.ColumnSpan = 2;
            row2.Cells.Add(source_cell);
            String course = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                course = crf.BasicInfoes.First().Course.ToString();
            row2.Cells.Add(createCell("病程：" + course + "月", true, Unit.Parse("25%"), HorizontalAlign.Left));
            table.Rows.Add(row2);

            TableRow row3 = new TableRow();
            String family = "无";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0 && crf.BasicInfoes.First().FamilyHistories != null && crf.BasicInfoes.First().FamilyHistories.Count > 0)
            {
                family = "";
                foreach (FamilyHistory fh in crf.BasicInfoes.First().FamilyHistories)
                {
                    family += fh.Content + "；";
                }
            }
            TableCell familyCell = createCell("相关家族史：" + family , true , Unit.Parse("100%") ,HorizontalAlign.Left);
            familyCell.ColumnSpan = 4;
            row3.Cells.Add(familyCell);
            table.Rows.Add(row3);

            TableRow row4 = new TableRow();
            String allergyHistory = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0 && crf.BasicInfoes.First().AllergyHistories != null && crf.BasicInfoes.First().AllergyHistories.Count > 0)
            {
                allergyHistory = "";
                foreach (AllergyHistory ah in crf.BasicInfoes.First().AllergyHistories)
                {
                    allergyHistory += ah.Content + "；";
                }
            }
            TableCell allergyHistoryCell = createCell("药物过敏史：" + allergyHistory, true, Unit.Parse("100%"), HorizontalAlign.Left);
            allergyHistoryCell.ColumnSpan = 4;
            row4.Cells.Add(allergyHistoryCell);
            table.Rows.Add(row4);

            TableRow row5 = new TableRow();
            String WestDiagnose = "";
            if(crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                WestDiagnose = crf.BasicInfoes.First().WestDiagnose;
            TableCell WestDiagnoseCell = createCell("西医诊断：" + WestDiagnose , true , Unit.Parse("100%") , HorizontalAlign.Left);
            WestDiagnoseCell.ColumnSpan = 4;
            row5.Cells.Add(WestDiagnoseCell);
            table.Rows.Add(row5);

            //病理
            TableRow row6 = new TableRow();
            TableCell PathologyCell = new TableCell();
            PathologyCell.ColumnSpan = 4;
            PathologyCell.BorderStyle = BorderStyle.Solid;
            PathologyCell.BorderColor = System.Drawing.Color.Black;
            PathologyCell.BorderWidth = Unit.Parse("1px");
            PathologyCell.Controls.Clear();
            PathologyCell.Controls.Add(createPathologyTable(crf));
            row6.Cells.Add(PathologyCell);
            table.Rows.Add(row6);

            TableRow row7 = new TableRow();
            String kps = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                kps = crf.BasicInfoes.First().KPS.ToString();
            TableCell kpsCell = createCell("KPS:" + kps, true, Unit.Parse("50%"), HorizontalAlign.Left);
            kpsCell.ColumnSpan = 2;
            row7.Cells.Add(kpsCell);
            String ecog = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                ecog = crf.BasicInfoes.First().ECOG.ToString();
            TableCell ECOGCell = createCell("ECOG:" + ecog , true,Unit.Parse("50%") , HorizontalAlign.Left);
            ECOGCell.ColumnSpan = 2;
            row7.Cells.Add(ECOGCell);
            table.Rows.Add(row7);

            TableRow row8 = new TableRow();
            String tnm = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                tnm = crf.BasicInfoes.First().TNM;
            TableCell TNMCell = createCell("TNM:" + tnm, true, Unit.Parse("50%"), HorizontalAlign.Left);
            TNMCell.ColumnSpan = 2;
            row8.Cells.Add(TNMCell);
            String dukes = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                dukes = crf.BasicInfoes.First().Dukes;
            TableCell DukesCell = createCell("Dukes:" + dukes, true, Unit.Parse("50%"), HorizontalAlign.Left);
            DukesCell.ColumnSpan = 2;
            row8.Cells.Add(DukesCell);
            table.Rows.Add(row8);

            TableRow row9 = new TableRow();
            TableCell chemosCell = createCell("",true,Unit.Parse("100%"),HorizontalAlign.Center);
            chemosCell.ColumnSpan = 4;
            row9.Controls.Add(chemosCell);
            Label lbl_Chemos = new Label();
            lbl_Chemos.Text = "化疗";
            chemosCell.Controls.Add(lbl_Chemos);
            ICollection<BasicChemo> bcs = null;
            if(crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                bcs = crf.BasicInfoes.First().Chemos;
            chemosCell.Controls.Add(createChemosTable(bcs));
            table.Rows.Add(row9);

            TableRow row10 = new TableRow();
            TableCell RadiosCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            RadiosCell.ColumnSpan = 4;
            row10.Cells.Add(RadiosCell);
            Label lbl_Radios = new Label();
            lbl_Radios.Text = "放疗";
            RadiosCell.Controls.Add(lbl_Radios);
            ICollection<BasicRadio> brs = null;
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                brs = crf.BasicInfoes.First().Radios;
            RadiosCell.Controls.Add(createRadiosTable(brs));
            table.Rows.Add(row10);

            TableRow row11 = new TableRow();
            TableCell OtherCuresCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            OtherCuresCell.ColumnSpan = 4;
            row11.Cells.Add(OtherCuresCell);
            Label lbl_OtherCures = new Label();
            lbl_OtherCures.Text = "其他";
            OtherCuresCell.Controls.Add(lbl_OtherCures);
            ICollection<BasicOtherCure> bocs = null;
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                bocs = crf.BasicInfoes.First().OtherCures;
            OtherCuresCell.Controls.Add(createBasicOtherCuresTable(bocs));
            table.Rows.Add(row11);

            TableRow row12 = new TableRow();
            TableCell OtherMergesCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            OtherMergesCell.ColumnSpan = 4;
            row12.Cells.Add(OtherMergesCell);
            Label lbl_OtherMerges = new Label();
            lbl_OtherMerges.Text = "是否合并其它疾病及用药";
            OtherMergesCell.Controls.Add(lbl_OtherMerges);
            ICollection<BasicOtherMerge> boms = null;
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                boms = crf.BasicInfoes.First().OtherMerges;
            OtherMergesCell.Controls.Add(createOtherMergesTable(boms));
            table.Rows.Add(row12);

            TableRow row13 = new TableRow();
            TableCell PhysiqueCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            PhysiqueCell.ColumnSpan = 4;
            row13.Cells.Add(PhysiqueCell);
            Label lbl_Physique = new Label();
            lbl_Physique.Text = "一般体格检查";
            PhysiqueCell.Controls.Add(lbl_Physique);
            BasicInfo bi = null;
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                bi = crf.BasicInfoes.First();
            PhysiqueCell.Controls.Add(createPhysiqueTable(bi));
            table.Rows.Add(row13);

            return table;
        }

        //病理
        private Table createPathologyTable(CRF crf)
        {
            Table table = new Table();

            TableRow caseCodeRow = new TableRow();
            String caseCode = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                caseCode = crf.BasicInfoes.First().CaseCode;
            caseCodeRow.Cells.Add(createCell("病历号：" + caseCode,false,Unit.Parse("100%"),HorizontalAlign.Left));
            table.Rows.Add(caseCodeRow);

            TableRow pathologyCodeRow = new TableRow();
            String pathologyCode = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                pathologyCode = crf.BasicInfoes.First().PathologyCode;
            pathologyCodeRow.Cells.Add(createCell("病理号：" + pathologyCode, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(pathologyCodeRow);

            TableRow operationHospitalRow = new TableRow();
            String operationHospital = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                operationHospital = crf.BasicInfoes.First().OperationHospital;
            operationHospitalRow.Cells.Add(createCell("手术医院：" + operationHospital, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(operationHospitalRow);

            TableRow operationTimeRow = new TableRow();
            String operationTime = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                operationTime = crf.BasicInfoes.First().OperationTime.ToString();
            operationTimeRow.Cells.Add(createCell("手术时间：" + operationTime, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(operationTimeRow);

            TableRow tumorRow = new TableRow();
            String tumor = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                tumor = crf.BasicInfoes.First().TumorLength + "*" + crf.BasicInfoes.First().TumorWidth + "*" + crf.BasicInfoes.First().TumorHeight + "cm";
            tumorRow.Cells.Add(createCell("肿块大小：" + tumor, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(tumorRow);

            TableRow tumorPlaceRow = new TableRow();
            String tumorPlace = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0){
                tumorPlace = TumorPlaceTypeInfo.GetName(crf.BasicInfoes.First().TumorPlace).ToString();
                if((TumorPlaceTypes)crf.BasicInfoes.First().TumorPlace == TumorPlaceTypes.ZC)
                    tumorPlace += "距肛门距离" + crf.BasicInfoes.First().Distance;
            }
            tumorPlaceRow.Cells.Add(createCell("癌肿部位：" + tumorPlace, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(tumorPlaceRow);

            TableRow pathologyTypeRow = new TableRow();
            String pathologyType = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                pathologyType = PathologyTypeInfo.GetName(crf.BasicInfoes.First().PathologyType);
            pathologyTypeRow.Cells.Add(createCell("病理学类型：" + pathologyType, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(pathologyTypeRow);

            TableRow InfiltrationPlaceRow = new TableRow();
            String infiltrationPlace = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                infiltrationPlace = InfiltrationPlaceTypeInfo.GetName(crf.BasicInfoes.First().InfiltrationPlace);
            InfiltrationPlaceRow.Cells.Add(createCell("浸润部位：" + infiltrationPlace, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(InfiltrationPlaceRow);

            TableRow splitRow = new TableRow ();
            String split = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                split = SplitTypeInfo.GetName(crf.BasicInfoes.First().Split);
            splitRow.Cells.Add(createCell("分化：" + split, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(splitRow);

            TableRow lymphMoveRow = new TableRow();
            String lymphMove = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                lymphMove = ((LymphMoveTypes)crf.BasicInfoes.First().LymphMove).ToString();
            lymphMoveRow.Cells.Add(createCell("淋巴结转移：" + lymphMove, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(lymphMoveRow);

            TableRow cutEdgeRow = new TableRow();
            String cutEdge = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                cutEdge = "上切缘" + TopCutEdgeTypeInfo.GetName(crf.BasicInfoes.First().TopCutEdge) + "下切缘" + BottomCutEdgeTypeInfo.GetName(crf.BasicInfoes.First().BottomCutEdge);
            cutEdgeRow.Cells.Add(createCell("淋巴结转移：" + cutEdge, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(cutEdgeRow);

            TableRow pulseEmbolusRow = new TableRow();
            String pulseEmbolus = "";
            if (crf.BasicInfoes != null && crf.BasicInfoes.Count > 0)
                pulseEmbolus = PulseEmbolusTypeInfo.GetName(crf.BasicInfoes.First().PulseEmbolus);
            pulseEmbolusRow.Cells.Add(createCell("脉管内癌栓：" + pulseEmbolus, false, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(pulseEmbolusRow);

            return table;
        }

        //化疗
        private Table createChemosTable(ICollection<BasicChemo> bcs)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (bcs == null || bcs.Count < 1)
                return table;
            foreach(BasicChemo bc in bcs){
                TableRow row = new TableRow();
                row.Cells.Add(createCell("方案：" + ChemoSchemeTypeInfo.GetName((byte)bc.Scheme),false,Unit.Parse("25%") , HorizontalAlign.Left));
                row.Cells.Add(createCell("开始时间：" + bc.StartTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("结束时间：" + bc.EndTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + bc.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }
            
            return table;
        }

        //其他
        private Table createBasicOtherCuresTable(ICollection<BasicOtherCure> ocs)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (ocs == null || ocs.Count < 1)
                return table;
            foreach (BasicOtherCure oc in ocs)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell("名称：" + oc.Name, false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("开始时间：" + oc.StartTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("结束时间：" + oc.EndTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + oc.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        //放疗
        private Table createRadiosTable(ICollection<BasicRadio> rs)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (rs == null || rs.Count < 1)
                return table;
            foreach (BasicRadio r in rs)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell("部位：" + RadioPlaceTypeInfo.GetName((byte)r.Place).ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("剂量：" + r.Dose.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("次数：" + r.Times.ToString().ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + r.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        //其他用药
        private Table createOtherMergesTable(ICollection<BasicOtherMerge> oms)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (oms == null || oms.Count < 1)
                return table;
            TableRow head_row = new TableRow();
            head_row.Cells.Add(createCell("疾病诊断", false, Unit.Parse("20%"), HorizontalAlign.Left));
            head_row.Cells.Add(createCell("药物通用名", false, Unit.Parse("20%"), HorizontalAlign.Left));
            head_row.Cells.Add(createCell("日剂量", false, Unit.Parse("15%"), HorizontalAlign.Left));
            head_row.Cells.Add(createCell("用药起止时间", false, Unit.Parse("25%"), HorizontalAlign.Left));
            head_row.Cells.Add(createCell("效果", false, Unit.Parse("20%"), HorizontalAlign.Left));
            table.Rows.Add(head_row);
            foreach (BasicOtherMerge om in oms)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell(DiagnoseTypeInfo.GetName((byte)om.Diagnose).ToString(), false, Unit.Parse("20%"), HorizontalAlign.Left));
                row.Cells.Add(createCell(om.DrugName, false, Unit.Parse("20%"), HorizontalAlign.Left));
                row.Cells.Add(createCell(om.Dose, false, Unit.Parse("15%"), HorizontalAlign.Left));
                row.Cells.Add(createCell(om.StartTime.ToString() + "-" + om.EndTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell(om.Effect, false, Unit.Parse("20%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        private Table createPhysiqueTable(BasicInfo bi)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            TableRow row1= new TableRow();
            row1.Cells.Add(createCell("体温：" + (bi == null ? "" : bi.Temperature.ToString()), false, Unit.Parse("50%"), HorizontalAlign.Left));
            row1.Cells.Add(createCell("心率：" + (bi == null ? "" : bi.HeartRate.ToString()), false, Unit.Parse("50%"), HorizontalAlign.Left));
            table.Rows.Add(row1);

            TableRow row2 = new TableRow();
            row2.Cells.Add(createCell("呼吸：" + (bi == null ? "" : bi.Breath.ToString()), false, Unit.Parse("50%"), HorizontalAlign.Left));
            row2.Cells.Add(createCell("血压：" + (bi == null ? "" : bi.MaxBP.ToString()) + "/" + (bi == null ? "" : bi.MinBP.ToString()), false, Unit.Parse("50%"), HorizontalAlign.Left));
            table.Rows.Add(row2);

            return table;
        }

        private TableCell createCell(String txt, Boolean haveborder, Unit width, HorizontalAlign align)
        {
            TableCell cell = new TableCell();
            cell.Text = txt;
            if (haveborder)
            {
                cell.BorderColor = System.Drawing.Color.Black;
                cell.BorderStyle = BorderStyle.Solid;
                cell.BorderWidth = Unit.Parse("1px");
            }
            if (width != null)
            {
                cell.Width = width;
            }
            cell.HorizontalAlign = align;
            return cell;
        }

    }
}